package cn.fhkj.web.controller;

import cn.fhkj.bean.ResultBean;
import cn.fhkj.config.OhMyEmail;
import cn.fhkj.dao.RenqiuZfPublicityReconsiderationMapper;
import cn.fhkj.entity.RenqiuZfPublicityReconsideration;
import cn.fhkj.exception.SendMailException;
import io.swagger.annotations.ApiOperation;
import jetbrick.template.JetEngine;
import jetbrick.template.JetTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.StringWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static cn.fhkj.config.OhMyEmail.SMTP_PLUSLEGAL;

/**
 * @program: renqiu-publicity-web
 * @description: 导出邮箱
 * @author: seven
 * @create: 2020-08-15 10:00
 **/
@RestController
@RequestMapping
public class EmailController extends BaseController{
    @Resource
    protected HttpServletRequest request;
    /**
     * 导出到邮箱
     * url:/api/send/{toEmail}
     *
     * @return
     */
    @ApiOperation("导出到邮箱")
    @GetMapping("/send/{toEmail}")
    public ResultBean<Object> sendEmail( @PathVariable String toEmail) {
        ResultBean<Object> resultBean = new ResultBean<>();
        HttpSession session = super.request.getSession();

        RenqiuZfPublicityReconsideration entity = new RenqiuZfPublicityReconsideration();
        entity.setApplicantName((String) session.getAttribute("applicantName"));
        entity.setApplicantSex((Integer) session.getAttribute("applicantSex"));
        entity.setApplicantBirthday((String) session.getAttribute("applicantName"));
        entity.setApplicantCerName((String) session.getAttribute("applicantName"));
        entity.setApplicantCerNum((String) session.getAttribute("applicantName"));
        entity.setApplicantWorkUnit((String) session.getAttribute("applicantName"));
        entity.setApplicantLocation((String) session.getAttribute("applicantName"));
        entity.setApplicantPostalCode((String) session.getAttribute("applicantName"));
        entity.setApplicantPhone((String) session.getAttribute("applicantName"));
        entity.setApplicantEmail((String) session.getAttribute("applicantName"));
        entity.setApplicantCompanyName((String) session.getAttribute("applicantName"));
        entity.setApplicantCompanyNum((String) session.getAttribute("applicantName"));
        entity.setApplicantPerson((String) session.getAttribute("applicantName"));
        entity.setApplicantLegalPerson((String) session.getAttribute("applicantName"));
        entity.setApplicantLegalPhone((String) session.getAttribute("applicantName"));
        entity.setApplicantLegalEmail((String) session.getAttribute("applicantName"));
        entity.setRespondentName((String) session.getAttribute("applicantName"));
        entity.setRespondentLegalName((String) session.getAttribute("applicantName"));
        entity.setRespondentLocation((String) session.getAttribute("applicantName"));
        entity.setRespondentPhone((String) session.getAttribute("applicantName"));
        entity.setAgentName((String) session.getAttribute("applicantName"));
        entity.setAgentNation((String) session.getAttribute("applicantName"));
        entity.setAgentSex((Integer) session.getAttribute("applicantName"));
        entity.setAgentVocation((String) session.getAttribute("applicantName"));
        entity.setAgentBirthday((String) session.getAttribute("applicantName"));
        entity.setAgentPostalCode((String) session.getAttribute("applicantName"));
        entity.setAgentLocation((String) session.getAttribute("applicantName"));
        entity.setAgentPhone((String) session.getAttribute("applicantName"));
        entity.setAgentFixedphone((String) session.getAttribute("applicantName"));
        entity.setAgentAddress((String) session.getAttribute("applicantName"));
        entity.setAgentCompanyName((String) session.getAttribute("applicantName"));
        entity.setAgentCompanyNum((String) session.getAttribute("applicantName"));
        entity.setAgentLegalPerson((String) session.getAttribute("applicantName"));
        entity.setAgentContacts((String) session.getAttribute("applicantName"));
        entity.setAgentContactsPhone((String) session.getAttribute("applicantName"));
        entity.setAgentContactsEmail((String) session.getAttribute("applicantName"));
        entity.setReconsiderationRequest((String) session.getAttribute("applicantName"));
        entity.setFactsEvidence((String) session.getAttribute("applicantName"));
        entity.setType((Integer) session.getAttribute("applicantName"));
        entity.setApplicantType((Integer) session.getAttribute("applicantName"));
        entity.setAgentType((Integer) session.getAttribute("applicantName"));
        entity.setStatus((Integer) session.getAttribute("applicantName"));

        if (entity != null ) {
            Map<String, Object> viewObject = new HashMap<>(4);
            viewObject.put("reconsideration", entity);
            OhMyEmail.config(SMTP_PLUSLEGAL(false), "hexc@pluslegal.cn", "Hexc@#123");
            JetEngine engine = JetEngine.create();
            JetTemplate template = engine.getTemplate("/templates/apply.jetx");
            StringWriter writer = new StringWriter();
            template.render(viewObject, writer);
            String output = writer.toString();
            try {
                //发送
                OhMyEmail.subject("这是您的行政复议申请")
                        .from("法加法律咨询")
                        .to(toEmail)
                        .html(output)
                        .send();
            } catch (SendMailException e) {

                resultBean.setMsg("发送邮件失败");
                resultBean.setSuccess(ResultBean.FAIL);
                return resultBean;
            }

            resultBean.setMsg("发送邮件成功");
            resultBean.setSuccess(ResultBean.SUCCESS);
            return resultBean;
        }

        resultBean.setMsg("数据不存在");
        resultBean.setSuccess(ResultBean.FAIL);
        return resultBean;
    }


}
